package com.jxb.myshop.dao.impl;

import com.jxb.myshop.common.Constant;
import com.jxb.myshop.dao.OrderDao;
import com.jxb.myshop.entity.Orders;
import com.jxb.myshop.entity.vo.OrderVo;
import com.jxb.myshop.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private QueryRunner qr = new QueryRunner(DBUtil.getDs());

    @Override
    public int createOrder(Orders order) throws SQLException {
        return qr.update("insert into orders values (?,?,?,?,?,?)",
                order.getOid(),
                order.getUid(),
                order.getAid(),
                order.getOcount(),
                order.getOtime(),
                order.getOstate());
    }

    @Override
    public List<OrderVo> getAllOrders(int uid) throws SQLException {
        return qr.query("select * from orders o,address a where o.aid = a.aid and o.uid = ?",
                new BeanListHandler<>(OrderVo.class),
                uid);
    }

    @Override
    public OrderVo getOrderAndAddress(String oid) throws SQLException {
        return qr.query("select * from orders o,address a where a.aid = o.aid and oid = ?",
                new BeanHandler<>(OrderVo.class),
                oid);
    }

    @Override
    public int changeStatus(String out_trade_no, int isSend) throws SQLException {
        return qr.update("update orders set ostate = ? where oid = ?",
                isSend,
                out_trade_no);
    }

    @Override
    public int changeStatus(String oid) throws SQLException {
        return qr.update("update orders set ostate = ? where oid = ?",
                Constant.Order.DONE,
                oid);
    }
}
